package com.tl.boot.service.menu.impl;

import com.tl.boot.common.enums.BooleanEnum;
import com.tl.boot.common.enums.StatusEnum;
import com.tl.boot.entity.menu.domain.MenuAfterLoginInfo;
import com.tl.boot.entity.menu.domain.MenuDetailInfo;
import com.tl.boot.entity.menu.domain.MenuInfo;
import com.tl.boot.entity.menu.domain.MenuResourceInfo;
import com.tl.boot.entity.menu.dto.AddMenuDTO;
import com.tl.boot.entity.menu.dto.ListMenuInfoDTO;
import com.tl.boot.entity.menu.dto.UpdateMenuBindResourceDTO;
import com.tl.boot.entity.menu.dto.UpdateMenuInfoDTO;
import com.tl.boot.entity.menu.vo.ListMenuInfoVO;
import com.tl.boot.entity.menu.vo.QueryMenuInfoVO;
import com.tl.boot.mapper.menu.MenuMapper;
import com.tl.boot.service.menu.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * com.tl.boot.service.menu.impl -- MenuServiceImpl
 * Description:
 * Created by joshua_liu <a href="mailto:joshualwork@163.com"></a>
 * On 2019/9/16 14:48
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public Long addMenuInfo(AddMenuDTO dto) {
        MenuInfo menuInfo = new MenuInfo();
        BeanUtils.copyProperties(dto, menuInfo);
        menuMapper.addMeanuInfo(menuInfo);
        if (dto.getResId() != null) {
            //添加menu和资源对应关系
            MenuResourceInfo menuResourceInfo = new MenuResourceInfo();
            menuResourceInfo.setMenuId(menuInfo.getMenuId());
            menuResourceInfo.setRelaStatus(Short.valueOf("1"));
            menuResourceInfo.setResId(dto.getResId());
            menuMapper.insertMenuResourceInfo(menuResourceInfo);
        }
        return menuInfo.getMenuId();
    }

    @Override
    public List<MenuAfterLoginInfo> parentMenuInfos(List<Long> parentIds) {
        return menuMapper.parentMenuInfos(parentIds);
    }

    @Override
    public List<ListMenuInfoVO> listMenuInfo(ListMenuInfoDTO dto) {
        MenuInfo menuInfo = new MenuInfo();
        BeanUtils.copyProperties(dto, menuInfo);
        return menuMapper.listMenuInfo(menuInfo);
    }

    @Override
    public void delMenuInfo(Long menuId) {
        menuMapper.delMenuInfo(menuId);
    }

    @Override
    public void updateMenuInfo(UpdateMenuInfoDTO dto) {
        MenuInfo menuInfo = new MenuInfo();
        BeanUtils.copyProperties(dto, menuInfo);
        menuMapper.updateMenuInfo(menuInfo);
    }

    @Override
    public QueryMenuInfoVO queryMenuInfo(Long menuId) {
        MenuDetailInfo menuInfo = menuMapper.queryMenuInfo(menuId);
        if (menuInfo != null) {
            QueryMenuInfoVO queryMenuInfoVO = new QueryMenuInfoVO();
            BeanUtils.copyProperties(menuInfo, queryMenuInfoVO);
            queryMenuInfoVO.setMenuStatusEnum(StatusEnum.getInstanceByCode(menuInfo.getMenuStatus().intValue()));
            queryMenuInfoVO.setIsLeafEnum(BooleanEnum.getInstanceByCode(menuInfo.getIsLeaf().intValue()));
            return queryMenuInfoVO;
        }
        return null;
    }

    @Override
    public Long updateMenuBindResource(UpdateMenuBindResourceDTO dto) {
        MenuResourceInfo menuResourceInfo = new MenuResourceInfo();
        menuResourceInfo.setResId(dto.getTargetResourceId());
        if (dto.getMrId() != null) {
            //更新绑定关系
            menuMapper.updateMenuResourceBindInfo(menuResourceInfo);
        } else {
            //添加绑定关系
            menuResourceInfo.setMenuId(dto.getMenuId());
            menuResourceInfo.setRelaStatus(Short.valueOf("1"));
            menuMapper.insertMenuResourceInfo(menuResourceInfo);
        }
        return menuResourceInfo.getMrId();
    }

    @Override
    public List<MenuAfterLoginInfo> queryMenuAfterLoginInfo(List<Long> resultMenuIds, List<Long> hasPermissionIds) {
        return menuMapper.queryMenuAfterLoginInfo(resultMenuIds,hasPermissionIds);
    }
}
